#include "CCharacter.h"
#include "CEventSystem.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
CCharacter::CCharacter()
{
	CEventSystem::GetInstance()->RegisterClient("MoveUp",this);
	CEventSystem::GetInstance()->RegisterClient("MoveDown",this);
	CEventSystem::GetInstance()->RegisterClient("MoveLeft",this);
	CEventSystem::GetInstance()->RegisterClient("MoveRight",this);
	CEventSystem::GetInstance()->RegisterClient("ButtonD",this);
	bFlipped = false;
	cTeam = TEAM_NULL;
	bUp = false;
	bDown = false;
	bLeft = false;
	bRight = false;
	bA = false;
	bB = false;
	bC = false;
	bD = false;
	bControl = true;
	cState = STATE_STAND;
	pAnim = new AnimInfo;
	//anim->SetOwner(this);
	floor = 500;
	CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Stand",pAnim);

	//Fake loaded stats
	fWalkVelFx =50;
	fWalkVelFy =0;
	fWalkVelBx =-50;
	fWalkVelBy=0;
	fRunVelFx=0;
	fRunVelFy=0;
	fRunVelBx=0;
	fRunVelBy=0;
	fJumpVelNx=0;
	fJumpVelNy=-200;
	fJumpVelFx=0;
	fJumpVelFy=-200;
	fJumpVelBx=0;
	fJumpVelBy=-200;
	fRunJumpVelFx=0;
	fRunJumpVelFy=0;
	fRunJumpVelBx=0;
	fRunJumpVelBy=0;
	fAirJumpVelNx=0;
	fAirJumpVelNy=0;
	fAirJumpVelFx=0;
	fAirJumpVelFy=0;
	fAirJumpVelBx=0;
	fAirJumpVelBy=0;
	ucAirJumpNum=0;
	ucAirJumpMax=1;
	fAirJumpTime=0;
	fStandFriction=0;
	fCrouchFriction=0;
	fGravityX=0;
	fGravityY=200;
}

CCharacter::~CCharacter()
{
	CEventSystem::GetInstance()->UnregisterClient("MoveUp",this);
	CEventSystem::GetInstance()->UnregisterClient("MoveDown",this);
	CEventSystem::GetInstance()->UnregisterClient("MoveLeft",this);
	CEventSystem::GetInstance()->UnregisterClient("MoveRight",this);
	CEventSystem::GetInstance()->UnregisterClient("ButtonD",this);
	delete pAnim;
}
void CCharacter::Update(float fElapsedTime)
{
	CAnimationManager::GetInstance()->Update(pAnim,fElapsedTime);
	SetPosX(GetPosX() + (GetVelX() * fElapsedTime));
	SetPosY(GetPosY() + (GetVelY() * fElapsedTime));
	if(cState != STATE_AIR)
	{
		SetVelX(0);
		SetVelY(0);
	}
	else
	{
		SetVelY(GetVelY() +fGravityY* fElapsedTime);
		SetVelX(GetVelX() +fGravityX* fElapsedTime);
		if(GetVelY() > -1 && GetVelY() < 1)
		{
			if(CAnimationManager::GetInstance()->GetAnimID(pAnim) != m_szCharacter+"JumpForwardUp" &&CAnimationManager::GetInstance()->GetAnimID(pAnim) != m_szCharacter+"JumpForwardPeak" )
				CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"JumpUpPeak",pAnim);
			else
				CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"JumpForwardPeak",pAnim);
		}
		if(pAnim->GetFinished() && GetVelY() > 1)
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"JumpUpFall",pAnim);
	}
	if(GetPosY() > floor)
	{
		cState = STATE_CROUCH;
		CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Crouch",pAnim);
		SetPosY(floor);
	}
	if(CAnimationManager::GetInstance()->GetAnimID(pAnim) == m_szCharacter+"UseCard" && pAnim->GetFinished())
		bControl = true;
	HandleInput();
}
void CCharacter::Render()
{
	CAnimationManager::GetInstance()->Render(pAnim,GetPosX(),GetPosY(),1,0,bFlipped,D3DCOLOR_XRGB(255, 255, 255));
}
void CCharacter::HandleEvent(CEvent* pEvent)
{
	if(pEvent->GetEventID() == "MoveUp")
	{
		bUp = true;
	}
	else if(pEvent->GetEventID() == "MoveDown")
	{
		bDown = true;
	}
	else if(pEvent->GetEventID() == "MoveRight")
	{
		bRight = true;
	}
	else if(pEvent->GetEventID() == "MoveLeft")
	{
		bLeft = true;
	}
	else if(pEvent->GetEventID() == "ButtonD")
	{
		bD = true;
	}
}
void CCharacter::HandleInput()
{
	if(bRight)
	{
		if(!bControl)
			return;
		if(cState == STATE_STAND)
		{
			bFlipped = false;
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Walk",pAnim);
			SetVelX(fWalkVelFx);
			SetVelY(fWalkVelFy);
		}
		else if(cState == STATE_AIR)
			SetVelX(50);
		if(bDown)
		{
			if(cState == STATE_STAND)
			{
				if(cState != STATE_CROUCH)
					SetVelX(0);
				CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"CrouchStart",pAnim);
				cState = STATE_CROUCH;
				if(pAnim->GetFinished())
					CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Crouch",pAnim);
			}
		}
		else if(cState == STATE_CROUCH)
		{
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"CrouchStop",pAnim);
			if(pAnim->GetFinished())
			{
				cState = STATE_STAND;
			}
		}
		if(bUp&&cState != STATE_AIR)
		{
			cState = STATE_AIR;
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"JumpForwardUp",pAnim);
			SetVelX(fJumpVelFx);
			SetVelY(fJumpVelFy);
		}
	}
	else if(bLeft)
	{
		if(!bControl)
			return;
		if(cState == STATE_STAND)
		{
			bFlipped = true;
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Walk",pAnim);
			SetVelX(fWalkVelBx);
			SetVelY(fWalkVelBy);
		}
		else if(cState == STATE_AIR)
			SetVelX(-50);
		if(bDown)
		{
			if(cState == STATE_STAND)
			{
				if(cState != STATE_CROUCH)
					SetVelX(0);
				CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"CrouchStart",pAnim);
				cState = STATE_CROUCH;
				if(pAnim->GetFinished())
					CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Crouch",pAnim);
			}
		}
		else if(cState == STATE_CROUCH)
		{
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"CrouchStop",pAnim);
			if(pAnim->GetFinished())
			{
				cState = STATE_STAND;
			}
		}
		if(bUp&&cState != STATE_AIR)
		{
			cState = STATE_AIR;
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"JumpForwardUp",pAnim);
			SetVelX(fJumpVelFx);
			SetVelY(fJumpVelFy);
		}
	}
	else if(bDown)
	{
		if(!bControl)
			return;
		if(cState == STATE_STAND)
		{
			if(cState != STATE_CROUCH)
				CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"CrouchStart",pAnim);
			cState = STATE_CROUCH;
			if(pAnim->GetFinished())
				CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Crouch",pAnim);
		}
	}
	else if(bUp)
	{
		if(!bControl)
			return;
		if(cState != STATE_AIR)
		{
			cState = STATE_AIR;
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"JumpUp",pAnim);
			SetVelX(fJumpVelNx);
			SetVelY(fJumpVelNy);
		}
	}
	else
	{
		if(!bControl)
			return;
		if(cState == STATE_CROUCH)
		{
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"CrouchStop",pAnim);
			if(pAnim->GetFinished())
			{
				cState = STATE_STAND;
			}
		}
		else if(cState != STATE_AIR)
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"Stand",pAnim);
	}
	if(bD)
	{
		if(bControl)
		{
			bControl = false;
			CAnimationManager::GetInstance()->FindAnim(m_szCharacter+"UseCard",pAnim);
		}
	}
	bUp = false;
	bDown = false;
	bLeft = false;
	bRight = false;
	bA = false;
	bB = false;
	bC = false;
	bD = false;
}